package com.xlh.controller.courseTest;

import com.github.pagehelper.PageInfo;
import com.xlh.aop.PreAcquireLock;
import com.xlh.common.Result;
import com.xlh.common.ResultEnum;
import com.xlh.constant.ReportConstant;
import com.xlh.dto.course.ChapterBasicInfoDTO;
import com.xlh.dto.courseTest.ReportDTO;
import com.xlh.dto.courseTest.experiment.ExperimentChapterStatisticsDTO;
import com.xlh.param.courseTest.ExperimentRunJudgeParam;
import com.xlh.param.courseTest.ExperimentStatisticsAllParam;
import com.xlh.param.courseTest.ExperimentStatisticsParam;
import com.xlh.param.courseTest.ReportParam;
import com.xlh.pojo.courseTest.ExperimentStatisticsPass;
import com.xlh.security.UserInfoHolder;
import com.xlh.service.courseTest.ExperimentService;
import com.xlh.service.courseTest.ExperimentStatisticsStepPassService;
import com.xlh.virtualization.exception.CheckException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;


/**
 * @author zhr 2019/4/3
 */
@RestController
@Api(tags = "ExperimentReport", description = "实验报告")
@RequestMapping("/course/{courseId}")
public class ExperimentReportController {

    @Autowired
    private UserInfoHolder userInfoHolder;
    @Autowired
    private ExperimentService experimentService;
    @Autowired
    private ExperimentStatisticsStepPassService experimentStatisticsStepPassService;


    @ApiOperation("获取实验报告")
    @GetMapping("/chapter/{chapterId}/report")
    @PreAuthorize("@permissionValidator.isStudent()")
    public Result<ReportDTO> getReport(@PathVariable("chapterId") Long chapterId, @PathVariable("courseId") Long courseId) {
        return Result.success(experimentService.getReport(courseId, chapterId, userInfoHolder.getUserId()));
    }

    @ApiOperation("上传word")
    @PostMapping("/chapter/{chapterId}/report")
    @PreAuthorize("@permissionValidator.isStudent()")
    @PreAcquireLock(value = ReportConstant.UPDATE_EXPERIMENT_REPORT)
    public Result<String> uploadReport(
            @PathVariable("chapterId") Long chapterId,
            @PathVariable Long courseId,
            @RequestParam(value = "file", required = false) MultipartFile file) {
        return Result.success(experimentService.uploadReport(chapterId, courseId, file, userInfoHolder.getUserId(), userInfoHolder.getUser().getName()));
    }

    @ApiOperation("保存或提交markdown")
    @PutMapping("/chapter/{chapterId}/report")
    @PreAuthorize("@permissionValidator.isStudent()")
    public Result saveReport(@PathVariable("chapterId") Long chapterId, @PathVariable Long courseId, @RequestBody @Valid ReportParam param) {
        experimentService.saveReport(chapterId, courseId, param, userInfoHolder.getUserId());
        return Result.success();
    }
    @ApiOperation("实验通过保存")
    @PutMapping("/chapter/{taskId}/statistics/{classId}")
    @PreAuthorize("@permissionValidator.student")
    public Result experimentStatistics(@PathVariable("taskId") Long taskId, @PathVariable Long courseId, @PathVariable Long classId) {

        experimentService.saveExperimentTime(taskId, courseId, classId,userInfoHolder.getUserId(), ExperimentStatisticsPass.PASS);
        return Result.success();
    }

    @ApiOperation("步骤通过保存")
    @GetMapping("/saveStep")
    @PreAuthorize("@permissionValidator.student")
    public Result stepStatisticsPass(@Valid ExperimentRunJudgeParam experimentRunJudgeParam) {

        experimentStatisticsStepPassService.saveStepPass(experimentRunJudgeParam);
        return Result.success();
    }


    @ApiOperation("实验通过查询")
    @GetMapping("/chapter/{chapterId}/statistics/{classId}/queryexperimentStatistics")
    @PreAuthorize("@permissionValidator.student")
    public Result queryexperimentStatistics(@PathVariable("chapterId") Long chapterId, @PathVariable Long courseId, @PathVariable Long classId
    ) {

        return Result.success(experimentService.queryExperiment(chapterId, courseId, classId, userInfoHolder.getUserId()));
    }
    @ApiOperation("实验视频时长保存")
    @PutMapping("/chapter/{chapterId}/statistics/{classId}/experimentVideo")
    @PreAuthorize("@permissionValidator.student")
    public Result experimentVideo(@PathVariable("chapterId") Long chapterId, @PathVariable Long courseId, @PathVariable Long classId
            , @RequestParam String videotime, @RequestParam Long taskId, @RequestParam Long stepId, @RequestParam String  sumTime) {

        experimentService.saveExperimentTime(chapterId, courseId, classId,userInfoHolder.getUserId(),videotime, taskId,  stepId,   sumTime);
        return Result.success();
    }
    @ApiOperation("实验视频时长学习时长统计")
    @GetMapping("/chapter/{chapterId}/statistics/{classId}/experimentVideoAndStudyTime")
    @PreAuthorize("@permissionValidator.teacher")
    public Result<List<Map<String, Object>>> experimentVideoAndStudyTime(@PathVariable("chapterId") Long chapterId, @PathVariable Long courseId, @PathVariable Long classId
    ) {
        List<Map<String, Object>> list = experimentService.queryeExperimentTime(chapterId, courseId, classId, userInfoHolder.getUserId());
        return Result.success(list);
    }
    @ApiOperation("实验通过统计")
    @GetMapping("/chapter/statistics/{classId}/pass")
    @PreAuthorize("@permissionValidator.teacher")
    public Result<PageInfo<Map<String, Object>>> experimentStatisticsPass(@PathVariable Long courseId, @PathVariable Long classId, @RequestParam Integer pageSize, @RequestParam Integer currentPage ) {
        PageInfo<Map<String, Object>> mapPageInfo = experimentService.experimentStatisticsPass(null, courseId, classId, null, pageSize, currentPage);
        return Result.success(mapPageInfo);
    }

    @ApiOperation("根据章节查询任务信息")
    @GetMapping("/chapter/statistics/getChapterTaskId")
    @PreAuthorize("@permissionValidator.teacher")
    public Result getChapterTaskId(@RequestParam(required = true) Long chapterId, @RequestParam(required = true) Long classId){
        List<ChapterBasicInfoDTO> taskIds = experimentService.getChapterTaskId(chapterId, classId);
        return Result.success(taskIds);
    }

    @ApiOperation("根据任务ID查询任务通过信息")
    @GetMapping("/chapter/statistics/experimentChapterStatistics")
    @PreAuthorize("@permissionValidator.teacher")
    public Result experimentChapterStatistics(@RequestParam(required = true) Long taskId,
                                              @RequestParam(required = true) Long classId){
        ExperimentChapterStatisticsDTO experimentChapterStatistics = experimentService.experimentChapterStatistics(taskId, classId);
        return Result.success(experimentChapterStatistics);
    }

    @ApiOperation("学生通过章节情况下载")
    @PostMapping("/chapter/statistics/chapterStatisticsDownLoad")
    @PreAuthorize("@permissionValidator.teacher")
    public Result chapterStatisticsDownLoad(@RequestParam(required = true) Long chapterId,
                                            @RequestParam(required = true) Long classId){
        boolean isSuccess = experimentService.chapterStatisticsDownLoad(chapterId, classId);
        if(isSuccess){
            return null;
        }
        return Result.success();
    }

    @ApiOperation("实验通过下载")
    @PostMapping("/chapter/statistics/{classId}/experimentStatisticsPassdownload")
    @PreAuthorize("@permissionValidator.teacher")
    public Result<List<Map<String,Object>>> experimentStatisticsPassdownload(@PathVariable Long courseId, @RequestBody(required=false ) List<ExperimentStatisticsParam> list ) {
        if (CollectionUtils.isEmpty(list)) {
            return Result.error(ResultEnum.SERVER_ERROR, "列表暂无数据");
        }
        experimentService.download(list);
        return Result.success();
    }
    @ApiOperation("实验统计下载")
    @PostMapping("/chapter/statistics/experimentStatisticsDown")
    @PreAuthorize("@permissionValidator.teacher")
    public Result<List<Map<String,Object>>> experimentStatisticsDown(@PathVariable Long courseId, @RequestBody ExperimentStatisticsAllParam experimentStatisticsAllParamParam, @RequestParam String courseName, @RequestParam String chapterName, @RequestParam String className ) {
        experimentService.downloadAll(experimentStatisticsAllParamParam, courseName,  chapterName,   className);
        return Result.success();
    }
    @ApiOperation("查询视频是否播放完")
    @GetMapping("/chapter/{chapterId}/statistics/{classId}/experimentVideoFind")
    @PreAuthorize("@permissionValidator.teacherOrStudent")
    public Result<String> experimentVideoFind(@PathVariable("chapterId") Long chapterId, @PathVariable Long courseId, @PathVariable Long classId
            , @RequestParam Long taskId, @RequestParam Long stepId) {
        return Result.success(experimentService.experimentStatisticsVideoFind( chapterId,  courseId,  classId,  userInfoHolder.getUserId(), taskId,  stepId));

    }
}
